<?php

namespace Common\TagLib;

use Think\Template\TagLib;

/**
 * 通用标签库解析类
 */
class icy8 extends TagLib
{
    // 标签定义
    protected $tags = array(
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'fetch' => array('attr' => 'file', 'close' => 0),
        'formItem' => array('attr' => 'label,inline,class,help,pane', 'close' => 1),
        'button' => array('attr' => 'class,name,title,type', 'close' => 0),
        'widget'=>['attr'=>'src,use','close'=>1],
        'w'=>['attr'=>'src','close'=>0],
    );

    public function _fetch($tag)
    {
        if ($tag['file']) {
            $file = '"' . $tag['file'] . '"'; // 文件名传入
        } else {
            $var = $tag['var']; // 变量传入
            $file = $this->autoBuildVar($var);
        }
        $code = '<?php
        $view = Think\Think::instance("Think\\View");
        if(' . $file . '){
            if(is_array(' . $file . ')):
                foreach(' . $file . ' as $key=>$v){
                    if($v && is_file($view->parseTemplate(' . $file . '))):
                        echo $view->fetch($v);
                    endif;
                }
            elseif(is_file($view->parseTemplate(' . $file . '))):
                echo $view->fetch(' . $file . '); endif;
        }
        ?>
';
        return $code;
    }

    public function _formItem($tag, $content)
    {
        $label = $tag['label'];
        if ($label[0] == '$') {
            $label = '{' . $label . '}';
        }
        $class = $tag['class'];
        $inline = $tag['inline'];
        $pane = $tag['pane'] ? 'pane' : '';
        $help = $tag['help'];
        if (strpos($help, '$') === 0) {
            $stmp = '<?php if(' . $help . '): ?>';
            $stmp2 = '<?php endif; ?>';
            $help = '{' . $help . '}';
        }
        $itemClass = $tag['item-class'];
        $labelDOM = '';
        if ($label) {
            $labelDOM .= "<label class=\"layui-form-label\">{$label}</label>";
        }
        if ($help) {
            $help = $stmp . '<div class="layui-form-mid layui-word-aux">' . $help . '</div>' . $stmp2;
        }
        $inputDOM = "<div class=\"{$class}\">{$content}</div>";
        if ($inline) {
            $html = <<<HTML
<div class="layui-form-item {$itemClass}" {$pane}>
    <div class="layui-inline">
        {$labelDOM}
        <div class="layui-input-inline">
            {$inputDOM}
        </div>
        {$help}
    </div>
</div>
HTML;

        } else {
            $html = <<<HTML
<div class="layui-form-item {$itemClass}" {$pane}>
    {$labelDOM}
    <div class="layui-input-block">
        {$inputDOM}
        {$help}
    </div>
</div>
HTML;
        }
        return $html;
    }

    public function _button($tag)
    {
        $name = $tag['name'];
        $type = $tag['type'] ?: 'submit';
        $title = $tag['title'];
        $class = $tag['class'];
        $btnType = $tag['btntype'] ? $tag['btntype'] : 'normal';
        $btnSize = $tag['btnsize'] ? $tag['btnsize'] : 'sm';
        $onclick = $tag['click'];
        $html = '<button class="layui-btn layui-btn-' . $btnSize . ' layui-btn-' . $btnType . ' ' . $class . '" type="' . $type . '" name="' . $name . '" onclick="' . $onclick . '">' . $title . '</button>';
        return $html;
    }

    /**
     * 组件调用的闭包方案
     * <web:widget src="Boot/Form/string">return ['name'=>'aaa'];</web:widget>
     * @param $tags
     * @param $content
     * @return string
     */
    public function _widget($tags, $content) {
        if ('$' == substr($tags['src'], 0, 1)) {
            $path = $this->autoBuildVar(substr($tags['src'], 1));
        } else {
            $path = '"' . $tags['src'] . '"';
        }
        if($tags['use']) {
            $use = 'use('.$tags['use'].')';
        }
        $code = <<<HTML
<?php \$func = function () {$use} { {$content} }; ?>
{:Widget({$path},\$func())}
HTML;
        return $code;
    }

    /**
     * 组件调用的简写方式
     * <web:w src="Boot/Form/string" param="$param"/>
     * @param $tags
     * @return string
     */
    public function _w($tags) {
        if ('$' == substr($tags['src'], 0, 1)) {
            $path = $this->autoBuildVar(substr($tags['src'], 1));
        } else {
            $path = '"' . $tags['src'] . '"';
        }
        if ('$' == substr($tags['param'], 0, 1)) {
            $param = $this->autoBuildVar(substr($tags['param'], 1));
        } else {
            // 暂时只接受变量传入
//            $param = 'json_decode("'.$tags['param'].'", true)';
        }
        $param = $param?:'[]';
        $code = '<?php echo Widget('.$path.','.$param.'); ?>';
        return $code;
    }
}

